/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package week4.Opdracht1;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import week4.Validation.ValidateXML;
import org.w3c.dom.DOMException;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import java.io.*;

/**
 *
 * @author 0833809
 */
public class Opdracht1 {

    /**
     *
     * het programma controleert XML document met behulp van het basis XML Schema document
    het programma toont de inhoud van het basis XML document (mbv gemaakt XSLT; output is in HTML)
    het programma verandert mbv XSLT bestand de inhoud van het basis XML document
    minimaal:
     *
    een tag toevoegen
    een attribuut toevoegen
    een tag en bijbehorende attribuut van naam veranderen
    weg laten van opgenomen commentaarregels
    overnemen van overige gegevens

     *
     *
     *
     * @param name
     * @param source
     * @return
     */
    static Document document;

    public static void main(String arg[]) {

        //-----------------week 2


        String pad = "";

        transfomer bb = new transfomer();

        System.out.println("xslt output");
        StringWriter outp = new StringWriter();


        String xmlpad = pad + "H:\\Mijn Documenten\\NetBeansProjects\\trunk\\infswa01\\src\\week3_xlst\\Opdracht3\\Opdracht3Fami.html";
        String xsltpad1 = pad + "H:\\Mijn Documenten\\NetBeansProjects\\trunk\\infswa01\\src\\week3_xlst\\Opdracht3\\XSLT Script.xml";

        //  String xsltpad2=pad+"/xmlHTMLStylusTotPrijs.xsl";

        System.out.println("begin:" + bb.toonXSLTString(xmlpad, xsltpad1) + "einde");

        //-----------




        String str = "H:\\Mijn Documenten\\NetBeansProjects\\trunk\\infswa01\\src\\week4\\Opdracht1\\Fami.xml";

        ValidateXML valideren = new ValidateXML();
        valideren.startValidation(str);

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {

            File fXmlFile = new File(str);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            NodeList nList = doc.getElementsByTagName("persoon");
            System.out.println("-----------------------");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    System.out.println("First Name : " + getTagValue("voornaam", eElement));
                    System.out.println("Last Name : " + getTagValue("letters", eElement));
                    System.out.println("Birth Day : " + getTagValue("geboortedatum", eElement));

                    //   System.out.println("Salary : " + getTagValue("salaris", eElement));
                    System.out.println("     ");

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }






//----------------begin toevoeging --------------

        try {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder domBuilder = domFactory.newDocumentBuilder();

            document = domBuilder.parse(str);
// System.out.println("test " + xmlpad + " is well-formed.");
        } catch (Exception e) {
            System.out.println("Parser error found: " + e.getMessage());

        }

        Element orderRoot = document.getDocumentElement();

//Bedrijfsgegevens bedrijf;
        String str_n = "persoon";

        NodeList nl_personen = document.getElementsByTagName(str_n);

        Node n_persoon;

        for (int i = 0; i < nl_personen.getLength(); i++) {
            if (nl_personen.item(i).getNodeType() == Node.ELEMENT_NODE) {

                System.out.println(nl_personen.item(i).getNodeName());

                n_persoon = nl_personen.item(i);

                Element npersoon = document.createElement("persoon");
                int persid = 6666;
                String persoonnummer = "" + persid;
                npersoon.setAttribute("persid", persoonnummer);
                npersoon.appendChild(document.createTextNode("2007-09-21"));
                orderRoot.appendChild(n_persoon);



                Element EL_ORDER = (Element) n_persoon;

                String str_ordernr = EL_ORDER.getAttribute("persid");

                Node n = n_persoon.getFirstChild();

                while (n != null) {

                    String str_nodenaam = n.getNodeName();
                    Element element = (Element) n;
                    String str_waarde = element.getFirstChild().getNodeValue();

                    if (("orderdatum".compareTo(str_nodenaam)) == 0) {
                        String str_orderdatum = str_waarde;
                    }

                    n = n.getNextSibling();
                }
            }
        }





    }

    private static String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
    }
}
